/*
 * @Author: xiaosihan 
 * @Date: 2024-10-05 23:12:00 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-20 17:34:45
 */

import { autorun } from "mobx";
import { ReactNode, useEffect, useRef, useState } from "react";
import styles from "./loginBox.module.less";
import { Button, Form, FormProps, Input, message } from "antd";
import factoryStore from "@views/factory/factoryStore";
import hashHistory from "@hashHistory";
import utils from "@utils";
import adminApi from "@api/adminApi";
import { get } from "lodash";
import adminStore from "@views/admin/adminStore";

interface Iprops {
    children?: ReactNode
}

// 登录组件
export default function LoginBox() {

    const dom = useRef<HTMLDivElement | null>(null);
    const [data, setData] = useState({});
    const [form] = Form.useForm();
    const [time, setTime] = useState(0);

    useEffect(() => {

    }, []);

    useEffect(() => autorun(() => {

    }, { delay: 10 }), []);

    //发送短信验证码
    const send_sms = async () => {
        form.validateFields(["phone"])
            .then(async () => {
                const phone = form.getFieldValue("phone");
                // const snedSMSResult = await loginApi.send_sms(phone);
                // timeDown(utils.isDev ? 5 : 60);
            }).catch(err => {
            })
    }

    // 倒计时
    const timeDown = (time: number) => {
        setTime(time);
        if (time > 0) {
            setTimeout(() => {
                timeDown(time - 1);
            }, 1000);
        }
    }

    // 登录
    const login = () => {
        form.validateFields()
            .then(async () => {
                const phone = form.getFieldValue("phone");
                const password = form.getFieldValue("password");
                adminStore.login(phone, password);
                // factoryStore.setIsLogin(true);
                // hashHistory.push("/factory/factoryQuotation");
                // const error = await userStore.loginByPhone(phone, code);
                // form.setFields([{
                //     name: ['code'],
                //     errors: [error], // 清空错误  
                // }]);
            }).catch(err => {
                debugger;
            })
    }

    type FieldType = {
        phone?: string;
        password?: string;
    };

    const onFinish: FormProps<FieldType>['onFinish'] = (values) => {
        console.log('Success:', values);
    };

    const onFinishFailed: FormProps<FieldType>['onFinishFailed'] = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };

    return (
        <div
            ref={dom}
            className={styles.loginBox}
        >
            <div className={styles.title} >管理员登录</div>
            <Form
                form={form}
                name="basic"
                initialValues={utils.isDev ? { phone: "15198054594", password: "753951" } : {}}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item<FieldType>
                    name="phone"
                    rules={[{ required: true, message: '请输入账号' }]}
                >
                    <Input className={styles.input} placeholder="账号" size="large" />
                </Form.Item>

                <Form.Item<FieldType>
                    name="password"
                    rules={[{ required: true, message: '请输入密码' }]}
                >
                    <Input.Password className={styles.input} placeholder="密码" size="large" />
                </Form.Item>

                {/* <div style={{ position: "relative" }} >
                    <Form.Item<FieldType>
                        name="code"
                        rules={[{ required: true, pattern: /^\d{6}$/, message: '请输入正确的验证码' }]}
                    >
                        <Input
                            className={styles.input}
                            size="large"
                            placeholder="请输入验证码"
                        />
                    </Form.Item>
                    <Button
                        size="large"
                        disabled={time > 0}
                        type="link"
                        className={styles.snedSMS}
                        onClick={() => send_sms()}
                    >发送验证码{time > 0 ? time : ""}</Button>
                </div> */}
            </Form>

            <Button
                className={styles.loginBtn}
                type="primary"
                block
                size="large"
                onClick={() => login()}
            >登录</Button>

            {/* <div className={styles.loginOther} >
                <span className={styles.loginWidthPassword} >账号密码登录</span>
            </div> */}
        </div>
    );

}